define(function (require, exports, module) {
    require('authorize');
    var $ = require("jquery");
    var dialog = require("dialog");
    var util = require("util");
    var ui = require("ui");
    var angular = require("angular");
    require("angular-route");
    require("angular-ui");
    require("angular-interceptor");
    require("uploader");
    require("/static_public/js/angular_directive/clipboard");
    require("/public/css/common/bootstrap-glyphicons/css/bootstrap-only-glyphicons.css");
    var environment = require('environment');
    var qmyxHost = environment.qmyx;

    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/para-setting/"; //api前缀
    angular
        .module("PublicInviteBroker", ['ngRoute', 'ngUI', 'ngInterceptor','angular.directive.clipboard'])
        .service("inviteBrokerSettingService", ['$http', function ($http) {
            return function () {
                return $http.get(api_prefix + 'get-invite-broker-data', {cache: false});
            }
        }])
        .controller("InviteBrokerController", ['$scope', 'inviteBrokerSettingService', '$http', function ($scope, inviteBrokerSettingService, $http) {
            var childCompanyRecord = function (record, isCheckAll, brokerIdentitySelectedAll) {
                var selected = record.selected;
                switch (record.Value) {
                    case '9003':
                        $scope.showAgentCompany = selected;
                        if (isCheckAll) {
                            $scope.data.agentCompany.selectedAll = selected;
                            checkAll($scope.data.agentCompany)
                        }
                        if (brokerIdentitySelectedAll) {
                            $scope.data.agentCompany.selectedAll = brokerIdentitySelectedAll;
                            checkAll($scope.data.agentCompany)
                        }
                        break;
                    case '9005':
                        $scope.showAgencyCompany = selected;
                        if (isCheckAll) {
                            $scope.data.agencyCompany.selectedAll = selected;
                            checkAll($scope.data.agencyCompany);
                        }
                        if (brokerIdentitySelectedAll) {
                            $scope.data.agencyCompany.selectedAll = brokerIdentitySelectedAll;
                            checkAll($scope.data.agencyCompany);
                        }
                        break;
                    case '9006':
                        $scope.showPartnerCompany = selected;
                        if (isCheckAll) {
                            $scope.data.partnerCompany.selectedAll = selected;
                            checkAll($scope.data.partnerCompany);
                        }
                        if (brokerIdentitySelectedAll) {
                            $scope.data.partnerCompany.selectedAll = brokerIdentitySelectedAll;
                            checkAll($scope.data.partnerCompany);
                        }
                        break;
                }
            };
            //经纪人类型全选取消
            $scope.updateBrokerIdentityCheckAll = function () {
                var data = $scope.data;

                if (data) {
                    var brokerIdentity = data.brokerIdentity;
                    var selectedAll = brokerIdentity.selectedAll;
                    for (var i = 0, item; item = brokerIdentity[i]; i++) {
                        item.selected = selectedAll;
                        childCompanyRecord(item, true);
                    }

                    if (data.agentCompany && data.agentCompany.enable) {
                        data.agentCompany.selectedAll = selectedAll;
                        $scope.updateAgentCompanyCheckAll();
                    }
                    if (data.agencyCompany && data.agencyCompany.enable) {
                        data.agencyCompany.selectedAll = selectedAll;
                        $scope.updateAgencyCompanyCheckAll();
                    }
                    if (data.partnerCompany && data.partnerCompany.enable) {
                        data.partnerCompany.selectedAll = selectedAll;
                        $scope.updatePartnerCompanyCheckAll();
                    }
                }
            };

            function isSelectedAll(list) {
                var checked;
                if (list && list.length > 0) {
                    checked = true;
                    for (var i = 0, item; item = list[i]; i++) {
                        if (!item.selected) {
                            checked = false;
                            break;
                        }
                    }
                } else {
                    checked = list.selectedAll;
                }

                return checked;
            }

            /**
             *
             * @param list
             * @returns {*}
             */
            function hasSelected(list) {
                var selected = false;
                if (list && list.length > 0) {
                    for (var i = 0, item; item = list[i]; i++) {
                        if (item.selected) {
                            selected = true;
                            break;
                        }
                    }
                } else {
                    selected = list.selectedAll;
                }

                return selected;
            }

            function setIdentitySelect(value, select) {
                var brokerIdentity = $scope.data.brokerIdentity;
                for (var i = 0, item; item = brokerIdentity[i]; i++) {
                    if (value === item.Value) {
                        item.selected = select;
                        break;
                    }
                }
            }

            function checkAll(agentCompany) {
                var selectedAll = agentCompany.selectedAll;
                for (var i = 0, item; item = agentCompany[i]; i++) {
                    item.selected = selectedAll;
                }
            }

            //经纪人类型同步全选/取消全选
            function brokerIdentityCheck() {
                var data = $scope.data;
                data.brokerIdentity.selectedAll =
                    isSelectedAll(data.brokerIdentity) &&
                    (!data.agentCompany.enable || isSelectedAll(data.agentCompany)) &&
                    (!data.agencyCompany.enable || isSelectedAll(data.agencyCompany) ) &&
                    (!data.partnerCompany.enable || isSelectedAll(data.partnerCompany));
            }

            $scope.updateBrokerIdentityCheck = function (record) {
                //含子公司的经济人类型
                childCompanyRecord(record, true);
                brokerIdentityCheck();
            };

            //代理公司全选/取消全选
            $scope.updateAgentCompanyCheckAll = function () {
                checkAll($scope.data.agentCompany);
                brokerIdentityCheck();

                var hasSelect = hasSelected($scope.data.agentCompany);
                setIdentitySelect('9003', hasSelect);
                $scope.showAgentCompany = hasSelect;
            };

            //代理公司同步全选/取消全选
            $scope.updateAgentCompanyCheck = function (item) {
                $scope.data.agentCompany.selectedAll = isSelectedAll($scope.data.agentCompany);
                brokerIdentityCheck();

                var hasSelect = hasSelected($scope.data.agentCompany);
                setIdentitySelect('9003', hasSelect);
                $scope.showAgentCompany = hasSelect;
            };

            //中介公司全选/取消全选
            $scope.updateAgencyCompanyCheckAll = function () {
                checkAll($scope.data.agencyCompany);
                brokerIdentityCheck();

                var hasSelect = hasSelected($scope.data.agencyCompany);
                setIdentitySelect('9005', hasSelect);
                $scope.showAgencyCompany = hasSelect;
            };

            //中介公司同步全选/取消全选
            $scope.updateAgencyCompanyCheck = function () {
                $scope.data.agencyCompany.selectedAll = isSelectedAll($scope.data.agencyCompany);
                brokerIdentityCheck();

                var hasSelect = hasSelected($scope.data.agencyCompany);
                setIdentitySelect('9005', hasSelect);
                $scope.showAgencyCompany = hasSelect;
            };

            //伙伴公司全选/取消全选
            $scope.updatePartnerCompanyCheckAll = function () {
                checkAll($scope.data.partnerCompany);
                brokerIdentityCheck();

                var hasSelect = hasSelected($scope.data.partnerCompany);
                setIdentitySelect('9006', hasSelect);
                $scope.showPartnerCompany = hasSelect;
            };

            //伙伴公司同步全选/取消全选
            $scope.updatePartnerCompanyCheck = function () {
                $scope.data.partnerCompany.selectedAll = isSelectedAll($scope.data.partnerCompany);
                brokerIdentityCheck();

                var hasSelect = hasSelected($scope.data.partnerCompany);
                setIdentitySelect('9006', hasSelect);
                $scope.showPartnerCompany = hasSelect;
            };

            //上传图片
            $scope.uploadImg = function () {
                $('#fileupload').fileupload({
                    dataType: 'json',
                    url: G_CONFIG["api_prefix"] + "common/file/upload-image-with-thumb",
                    add: function (e, data) {
                        data.submit();
                        $("#img_wrap").append('<div class="upload-process">上传中...</div>');
                    },
                    done: function (e, data) {
                        $("#img_wrap").find(".upload-process").remove();
                        var res = data.result;
                        if (res.retCode == "0") {
                            var img_url = res.data.path;
                            var thumbnail = res.data.thumbnail;
                            $scope.data.share_pic_url = img_url;
                            $scope.data.share_pic_name = res.data.name;
                            $scope.data.thumbnail = thumbnail;
                            $scope.$applyAsync();
                        } else {
                            $("#fileupload").nextAll(".js-err").html("上传失败:" + res.errMsg);
                            $("#share_invite_img").val("");
                        }
                    }
                });
            };

            function extractSelect(companies) {
                var str = '';
                if (companies.selectedAll) {
                    str = 'all';
                } else {
                    var arr = [];
                    for (var i = 0, item; item = companies[i]; i++) {
                        if (item.selected) {
                            arr.push(item.Value);
                        }
                    }
                    str = arr.join(',');
                }
                return str;
            }

            //保存邀请经纪人
            $scope.save = function () {
                var data = $scope.data;
                //待保存的数据
                var postData = {
                    token: util.getToken()
                };

                //是否开启邀请经纪人 0 关闭 1 开启
                if ($scope.showInvitebrokerMes) {
                    postData.enable = 1;

                    //经纪人身份
                    var brokerIdentity = data.brokerIdentity;
                    if (brokerIdentity.selectedAll) {
                        postData.brokerType = 'all';
                    } else {
                        var brokerIdentitySelects = [];
                        var item;
                        for (var i = 0; item = brokerIdentity[i]; i++) {
                            if (item.selected) {
                                brokerIdentitySelects.push(item.Value);
                            }
                        }

                        postData.brokerType = brokerIdentitySelects.join(',');
                    }
                    //邀请注册奖励
                    postData.bonus = data.inviteBrokerBonus;
                    //邀请经纪人活动规则设置
                    postData.rule = data.activity_rule;
                    //标题
                    postData.title = data.share_title;
                    //摘要
                    postData.summary = data.share_summary;
                    //图片路径
                    postData.urlPic = data.share_pic_url;
                    //缩略图
                    postData.thumbnail = data.thumbnail;
                    //图片名称
                    postData.picName = '上传.png';
                    //合作伙伴
                    postData.partnerCompany = extractSelect($scope.data.partnerCompany);
                    //代理公司
                    postData.agentCompany = extractSelect($scope.data.agentCompany);
                    //中介公司
                    postData.agencyCompany = extractSelect($scope.data.agencyCompany);
                    postData.regpage_button_name = data.regpage_button_name;
                    postData.regpage_background_img = data.regpage_background_img;
                    postData.regpage_button_color = data.regpage_button_color;
                    postData.regpage_font_color = data.regpage_font_color;
                } else {
                    postData.enable = 0;
                }
postData.XDEBUG_SESSION_START='netbeans-xdebug';

                $http.post(G_CONFIG.api_prefix + "qmyx/para-setting/save-invite-broker-data", postData)
                    .success(function (res) {
                        var retCode = res.retCode;
                        if (retCode == 0) {
                            ui.showSucTip();
                        }
                    })
            };

            function setSelectedStatus(companies, selects) {
                if (selects) {
                    var selectAll = selects.select === 'all';
                    for (var i = 0, item; item = companies[i]; i++) {
                        item.selected = selectAll || util.inArray(item.Value, selects.selected);
                    }

                    if (!selectAll) {
                        selectAll = isSelectedAll(selects);
                    }
                    companies.selectedAll = selectAll;
                }
            }

            inviteBrokerSettingService().success(function (res) {
                var data = res.data;

                var brokerIdentity = data.brokerIdentity;

                function inBroker(value) {
                    return util.inArray(value, brokerIdentity, function (id, item) {
                        return id === item.Value;
                    });
                }

                //代理公司
                setSelectedStatus(data.agentCompany, data.agentCompanySelect);
                data.agentCompany.enable = inBroker('9003');
                //中介公司
                setSelectedStatus(data.agencyCompany, data.agencyCompanySelect);
                data.agencyCompany.enable = inBroker('9005');
                //伙伴公司
                setSelectedStatus(data.partnerCompany, data.partnerCompanySelect);
                data.partnerCompany.enable = inBroker('9006');

                //选择启用该功能的经纪人类型
                var brokerIdentitySelect = data.brokerIdentitySelect;

                var brokerIdentitySelectAll;
                if (brokerIdentitySelect) {
                    brokerIdentitySelectAll = brokerIdentitySelect.select === 'all';
                } else {
                    brokerIdentitySelectAll = data.identity === 'all'
                }
                brokerIdentity.selectedAll = brokerIdentitySelectAll;

                data.activity_rule = data.activity_rule || '1、成功邀请好友注册成为全民经纪人，就有机会赢取奖励。\r\n2、邀请的好友成为经纪人之后，推荐客户成功购房，您也可以得到相应的佣金奖励。';
                //
                $scope.data = data;
                if(!$scope.data.regpage_background_img){
                    $scope.data.regpage_background_img = qmyxHost + '/page/parameter_settings/images/invite_reg.png'
                }

                for (var i = 0, item; item = brokerIdentity[i]; i++) {
                    item.selected = brokerIdentitySelectAll || (!!brokerIdentitySelect && util.inArray(item.Value, brokerIdentitySelect));
                    childCompanyRecord(item, false, brokerIdentity.selectedAll);
                }

                $scope.showInvitebrokerMes = $scope.data.enable == 1;
                //邀请经纪人功能开关
                ui.initSwitch("#invitebroker_btn", function (data) {
                    $scope.showInvitebrokerMes = data.value == 1;
                    $scope.$applyAsync();
                });
                //邀请注册奖励
                if ($scope.data.enable == 1) {
                    ui.openSwitch("invitebroker_btn");
                } else {
                    ui.offSwitch("invitebroker_btn");
                }

                //为了兼容万恶的IE
                //初始化按钮、字体颜色
                $('.fontcolor-picker').css({'background': $scope.data.font_color});
                $('.fontcolor-tmpl').css({'color': $scope.data.font_color});
                $('.buttoncolor-picker').css({'background': $scope.data.button_color});
                $('.buttoncolor-tmpl').css({'background': $scope.data.button_color});
            })

            $(document).on('click', function (e) {
                if (!$(e.target).closest('.font-board,.color-picker').length) {
                    $scope.isShowFontColorPicker = false;
                    $scope.isShowBtnColorPicker = false;
                    $scope.$applyAsync();
                }
            });

            $scope.showFontColorPicker = function(){
                $scope.isShowFontColorPicker = true;
                $scope.isShowBtnColorPicker = false;
            };
            $scope.showButtonColorPicker = function(){
                $scope.isShowFontColorPicker = false;
                $scope.isShowBtnColorPicker = true;
            };

            $scope.colors = [
                {value:'#ffffff',text:'白色'},
                {value:'#000000',text:'黑色'},
                {value:'#ff9f16',text:'桔色'},
                {value:'#f06387',text:'猩红'},
                {value:'#da6ce4',text:'酱紫'},
                {value:'#8b6af6',text:'紫色'},
                {value:'#5e58de',text:'蓝紫'},
                {value:'#0974cf',text:'海蓝'},
                {value:'#0eccca',text:'亮蓝'},
                {value:'#75b93e',text:'草绿'},
                {value:'#f7691c',text:'橙黄'},
                {value:'#0bafe5',text:'天蓝'},
                {text: '淡紫红',value:'#FFF0F5'},
                {text: '浅粉红',value:'#FFB6C1'},
                {text: '粉红',value:'#FFC0CB'},
                {text: '热情的粉红',value:'#FF69B4'},
                {text: '弱紫罗兰红',value:'#DB7093'},
                {text: '深粉红',value:'#FF1493'},
                {text: '中紫罗兰红',value:'#C71585'},
                {text: '深红(猩红)',value:'#DC143C'},
                {text: '淡紫色(熏衣草淡紫)',value:'#E6E6FA'},
                {text: '蓟色',value:'#D8BFD8'},
                {text: '洋李色(李子紫)',value:'#DDA0DD'},
                {text: '紫罗兰',value:'#EE82EE'},
                {text: '暗紫色(兰花紫)',value:'#DA70D6'},
                {text: '洋红(玫瑰红)',value:'#FF00FF'},
                {text: '紫红(灯笼海棠)',value:'#FF00FF'},
                {text: '中兰花紫',value:'#BA55D3'},
                {text: '中紫色',value:'#9370DB'},
                {text: '蓝紫罗兰',value:'#8A2BE2'},
                {text: '暗紫罗兰',value:'#9400D3'},
                {text: '暗兰花紫',value:'#9932CC'},
                {text: '深洋红',value:'#8B008B'},
                {text: '紫色',value:'#800080'},
                {text: '靛青/紫兰色',value:'#4B0082'},
                {text: '爱丽丝蓝',value:'#F0F8FF'},
                {text: '蔚蓝色',value:'#F0FFFF'},
                {text: '亮蓝',value:'#ADD8E6'},
                {text: '粉蓝色(火药青)',value:'#B0E0E6'},
                {text: '亮天蓝色',value:'#87CEFA'},
                {text: '天蓝色',value:'#87CEEB'},
                {text: '深天蓝',value:'#00BFFF'},
                {text: '矢车菊蓝',value:'#6495ED'},
                {text: '闪兰色(道奇蓝)',value:'#1E90FF'},
                {text: '皇家蓝/宝蓝',value:'#4169E1'},
                {text: '亮钢蓝',value:'#B0C4DE'},
                {text: '军兰色(军服蓝)',value:'#5F9EA0'},
                {text: '钢蓝/铁青',value:'#4682B4'},
                {text: '亮蓝灰(亮石板灰)',value:'#778899'},
                {text: '灰石色(石板灰)',value:'#708090'},
                {text: '中暗蓝色(中板岩蓝)',value:'#7B68EE'},
                {text: '石蓝色(板岩蓝)',value:'#6A5ACD'},
                {text: '暗灰蓝色(暗板岩蓝)',value:'#483D8B'},
                {text: '纯蓝',value:'#0000FF'},
                {text: '中蓝色',value:'#0000CD'},
                {text: '午夜蓝',value:'#191970'},
                {text: '暗蓝色',value:'#00008B'},
                {text: '海军蓝',value:'#000080'},
                {text: '淡青色',value:'#E0FFFF'},
                {text: '青色',value:'#00FFFF'},
                {text: '暗瓦灰色(暗石板灰)',value:'#2F4F4F'},
                {text: '暗青色',value:'#008B8B'},
                {text: '水鸭色',value:'#008080'},
                {text: '弱绿宝石',value:'#AFEEEE'},
                {text: '浅绿色(水色)',value:'#00FFFF'},
                {text: '宝石碧绿',value:'#7FFFD4'},
                {text: '中宝石碧绿',value:'#66CDAA'},
                {text: '绿宝石',value:'#40E0D0'},
                {text: '中绿宝石',value:'#48D1CC'},
                {text: '暗绿宝石',value:'#00CED1'},
                {text: '淡绿色',value:'#90EE90'},
                {text: '弱绿色',value:'#98FB98'},
                {text: '中春绿色',value:'#00FA9A'},
                {text: '春绿色',value:'#00FF7F'},
                {text: '浅海洋绿',value:'#20B2AA'},
                {text: '海洋绿',value:'#2E8B57'},
                {text: '中海洋绿',value:'#3CB371'},
                {text: '暗海洋绿',value:'#8FBC8F'},
                {text: '森林绿',value:'#228B22'},
                {text: '纯绿',value:'#008000'},
                {text: '暗绿色',value:'#006400'},
                {text: '闪光绿',value:'#00FF00'},
                {text: '闪光深绿',value:'#32CD32'},
                {text: '草绿色(草坪绿)',value:'#7CFC00'},
                {text: '黄绿色(查特酒绿)',value:'#7FFF00'},
                {text: '绿黄色',value:'#ADFF2F'},
                {text: '黄绿色',value:'#9ACD32'},
                {text: '浅黄色',value:'#FFFFE0'},
                {text: '玉米丝色',value:'#FFF8DC'},
                {text: '米色/灰棕色',value:'#F5F5DC'},
                {text: '亮菊黄',value:'#FAFAD2'},
                {text: '老花色(旧蕾丝)',value:'#FDF5E6'},
                {text: '亚麻布',value:'#FAF0E6'},
                {text: '柠檬绸',value:'#FFFACD'},
                {text: '番木色(番木瓜)',value:'#FFEFD5'},
                {text: '白杏色',value:'#FFEBCD'},
                {text: '陶坯黄',value:'#FFE4C4'},
                {text: '浅黄色(小麦色)',value:'#F5DEB3'},
                {text: '鹿皮色(鹿皮靴)',value:'#FFE4B5'},
                {text: '纳瓦白(土著白)',value:'#FFDEAD'},
                {text: '灰菊黄(苍麒麟色)',value:'#EEE8AA'},
                {text: '黄褐色(卡叽布)',value:'#F0E68C'},
                {text: '暗黄褐色(深卡叽布)',value:'#BDB76B'},
                {text: '纯黄',value:'#FFFF00'},
                {text: '金色',value:'#FFD700'},
                {text: '金菊黄',value:'#DAA520'},
                {text: '暗金菊黄',value:'#B8860B'},
                {text: '橄榄',value:'#808000'},
                {text: '橄榄褐色',value:'#6B8E23'},
                {text: '暗橄榄绿',value:'#556B2F'},
                {text: '橙色',value:'#FFA500'},
                {text: '茶色',value:'#D2B48C'},
                {text: '硬木色',value:'#DEB887'},
                {text: '沙棕色',value:'#F4A460'},
                {text: '巧克力色',value:'#D2691E'},
                {text: '秘鲁色',value:'#CD853F'},
                {text: '重褐色(马鞍棕色)',value:'#8B4513'},
                {text: '黄土赭色',value:'#A0522D'},
                {text: '浅玫瑰色(薄雾玫瑰)',value:'#FFE4E1'},
                {text: '桃肉色',value:'#FFDAB9'},
                {text: '浅鲑鱼肉色',value:'#FFA07A'},
                {text: '珊瑚',value:'#FF7F50'},
                {text: '深橙色',value:'#FF8C00'},
                {text: '淡珊瑚色',value:'#F08080'},
                {text: '玫瑰棕色',value:'#BC8F8F'},
                {text: '印度红',value:'#CD5C5C'},
                {text: '鲜肉/鲑鱼色',value:'#FA8072'},
                {text: '深鲜肉/鲑鱼色',value:'#E9967A'},
                {text: '番茄红',value:'#FF6347'},
                {text: '橙红色',value:'#FF4500'},
                {text: '纯红',value:'#FF0000'},
                {text: '棕色',value:'#A52A2A'},
                {text: '火砖色(耐火砖)',value:'#B22222'},
                {text: '深红色',value:'#8B0000'},
                {text: '栗色',value:'#800000'},
                {text: '纯白',value:'#FFFFFF'},
                {text: '雪白色',value:'#FFFAFA'},
                {text: '花的白色',value:'#FFFAF0'},
                {text: '象牙色',value:'#FFFFF0'},
                {text: '海贝壳',value:'#FFF5EE'},
                {text: '薄荷奶油',value:'#F5FFFA'},
                {text: '幽灵白',value:'#F8F8FF'},
                {text: '蜜色(蜜瓜色)',value:'#F0FFF0'},
                {text: '白烟',value:'#F5F5F5'},
                {text: '古董白',value:'#FAEBD7'},
                {text: '淡灰色(庚斯博罗灰)',value:'#DCDCDC'},
                {text: '浅灰色',value:'#D3D3D3'},
                {text: '银灰色',value:'#C0C0C0'},
                {text: '深灰色',value:'#A9A9A9'},
                {text: '灰色',value:'#808080'},
                {text: '暗淡的灰色',value:'#696969'},
                {text: '纯黑',value:'#000000'},
            ];

            $scope.setFontColor = function (color) {
                $scope.data.regpage_font_color = color;
                //为了兼容万恶的IE
                $('.fontcolor-picker').css({'background': color});
                $('.fontcolor-tmpl').css({'color': color});
            };
            $scope.setBtnColor = function (color) {
                $scope.data.regpage_button_color = color;
                //为了兼容万恶的IE
                $('.buttoncolor-picker').css({'background': color});
                $('.buttoncolor-tmpl').css({'background': color});
            };

            $scope.afterCopy = function () {
                util.alert('复制成功');
            };
        }]);
});